<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统主页</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .title {
            color: #333;
            margin: 0;
        }
        .logout-btn {
            background-color: #dc3545;
            color: white;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
        }
        .logout-btn:hover {
            background-color: #c82333;
        }
        .api-section {
            margin-bottom: 30px;
        }
        .api-section h3 {
            color: #333;
            margin-bottom: 15px;
        }
        .api-buttons {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        .api-btn {
            background-color: #007bff;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .api-btn:hover {
            background-color: #0056b3;
        }
        .result-area {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 15px;
            min-height: 100px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 14px;
        }
        .stats-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        .stats-table th,
        .stats-table td {
            border: 1px solid #dee2e6;
            padding: 8px 12px;
            text-align: left;
        }
        .stats-table th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 class="title">系统主页</h1>
            <a href="login?action=logout" class="logout-btn">退出登录</a>
        </div>
        
        <div class="api-section">
            <h3>API接口测试</h3>
            <div class="api-buttons">
                <button class="api-btn" onclick="callApi('/commService/userIp.do')">获取访问者IP</button>
                <button class="api-btn" onclick="callApi('/commService/userAgent.do')">获取浏览器信息</button>
                <button class="api-btn" onclick="callApi('/commService/cookies.do')">获取Cookie信息</button>
                <button class="api-btn" onclick="callApi('/commService/uirViews.do')">获取访问统计</button>
            </div>
            
            <h4>API响应结果:</h4>
            <div id="result" class="result-area">点击上方按钮调用API接口...</div>
        </div>
        
        <div class="api-section">
            <h3>接口访问统计</h3>
            <button class="api-btn" onclick="loadStats()">刷新统计数据</button>
            <table class="stats-table" id="statsTable" style="display: none;">
                <thead>
                    <tr>
                        <th>接口路径</th>
                        <th>访问次数</th>
                    </tr>
                </thead>
                <tbody id="statsBody">
                </tbody>
            </table>
        </div>
    </div>

    <script>
        function callApi(url) {
            fetch(url)
                .then(response => response.text())
                .then(data => {
                    document.getElementById('result').textContent = data;
                })
                .catch(error => {
                    document.getElementById('result').textContent = '请求失败: ' + error.message;
                });
        }
        
        function loadStats() {
            fetch('/commService/uirViews.do')
                .then(response => response.json())
                .then(data => {
                    const statsBody = document.getElementById('statsBody');
                    const statsTable = document.getElementById('statsTable');
                    
                    statsBody.innerHTML = '';
                    
                    data.forEach(item => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                            <td>${item.uri}</td>
                            <td>${item.viewNum}</td>
                        `;
                        statsBody.appendChild(row);
                    });
                    
                    statsTable.style.display = 'table';
                })
                .catch(error => {
                    alert('加载统计数据失败: ' + error.message);
                });
        }
        
        // 页面加载时自动加载统计数据
        window.onload = function() {
            loadStats();
        };
    </script>
</body>
</html>
